package org.jeecg.modules.app.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.utils.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.modules.app.entity.Customer;
import org.jeecg.modules.app.entity.ShopInfoRemake;
import org.jeecg.modules.app.service.CustomerService;
import org.jeecg.modules.app.service.OrderService;
import org.jeecg.modules.app.service.ShopInfoRemakeService;
import org.jeecg.modules.app.service.ShopService;
import org.jeecg.modules.app.utils.JwtUtil;
import org.jeecg.modules.app.utils.MD5Util;
import org.jeecg.modules.app.vo.LoginCustomer;
import org.jeecg.modules.app.vo.RegisterCustomer;
import org.jeecg.modules.app.vo.RemakeVo;
import org.jeecg.modules.app.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @author 李圳川
 * @version 1.0
 * @date 2022/6/9 0:26
 * @content
 */
@RestController
@RequestMapping("/schoolOrder/app")
@Api(tags="app顾客信息和顾客登录")
@Slf4j
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private ShopInfoRemakeService shopInfoRemakeService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * app登录接口
     * @param loginCustomer
     * @param request
     * @return
     */
    @ApiOperation("app顾客登录接口")
    @PostMapping(value = "/login")
    public Result<?> login(@RequestBody LoginCustomer loginCustomer, HttpServletRequest request){
        String historyToken = request.getHeader("token");
        if(StringUtils.isNotEmpty(historyToken)){
            redisTemplate.delete("token:"+historyToken);
        }
        // 正常登录
        Customer customer = customerService.login(loginCustomer);
        if (customer == null) {
            return Result.error("500","账号或密码错误!");
        } else {
            String token = JwtUtil.jwt(customer);
            customer.setPassword(MD5Util.encryptPassword(customer.getPassword()));
            redisTemplate.opsForValue().set("token:"+token,customer,3, TimeUnit.HOURS);
            return Result.success("200","登录成功", token);
        }
    }

    /**
     * app顾客注册接口
     * @param registerCustomer
     * @return
     */
    @ApiOperation("app顾客注册接口")
    @PostMapping(value = "/register")
    public Result<?> register(@RequestBody RegisterCustomer registerCustomer){
            Boolean isRegister = customerService.register(registerCustomer);
            if(isRegister) {
                return Result.success("200","注册成功！");
            }else{
                return Result.error("500","用户名已存在！");
            }
    }

    /**
     * 修改顾客信息
     * @param customer
     * @param request
     * @return
     */
    @ApiOperation("app修改顾客信息")
    @PostMapping(value = "/updateCustomer")
    public Result<?> updateCustomer(@RequestBody Customer customer,HttpServletRequest request){

        String historyToken = request.getHeader("token");
        if(StringUtils.isNotEmpty(historyToken)){
            redisTemplate.delete("token:"+historyToken);
        }
        customerService.updateCustomer(customer);
        String token = JwtUtil.jwt(customer);
        customer.setPassword(MD5Util.encryptPassword(customer.getPassword()));
        redisTemplate.opsForValue().set("token:"+token,customer,3, TimeUnit.HOURS);
        return Result.success("200","修改成功！", token);

    }


    /**
     * 查询顾客的评价
     * @param id
     * @return
     */
    @ApiOperation("app查询顾客的评价")
    @GetMapping(value = "/selectCustomerRemakeById")
    public Result<?> selectCustomerRemakeById(String id){
        List<RemakeVo> remakeVoList = customerService.selectCustomerRemakeById(id);
        return Result.success("200","查询成功",remakeVoList);
    }

    /**
     * app顾客评论订单
     * @param shopInfoRemake
     * @return
     */
    @ApiOperation("app顾客评价订单")
    @PostMapping(value = "/evaluate")
    public Result<?> evaluate(@RequestBody ShopInfoRemake shopInfoRemake){
        shopInfoRemakeService.insert(shopInfoRemake);
      //  System.out.println(shopInfoRemake);
        RemakeVo remakeVo = new RemakeVo();
        remakeVo.setCustomerId(shopInfoRemake.getCustomerId());
        remakeVo.setOrderId(shopInfoRemake.getOrderId());
        remakeVo.setShopId((shopInfoRemake.getShopId()));
        remakeVo.setServiceScore(shopInfoRemake.getServiceScore());
        remakeVo.setFoodScore(shopInfoRemake.getFoodScore());
        shopService.upDateShopInfoScore(remakeVo);
        orderService.updateOrderById(shopInfoRemake.getOrderId(),"7");
        return Result.success("200","评价成功！");

    }


    /**
     * 获取当前app登录用户的信息
     * @param request
     * @return
     */
    @ApiOperation("app获取登录顾客的信息")
    @GetMapping(value = "/getCustomerByToken")
    public Result<?> getCustomerByToken(HttpServletRequest request){
        String token = request.getHeader("token");
        if(JwtUtil.checkToken(token)){
            Customer customer = this.customerService.getCustomerByToken(token);
            return Result.success("200",customer);
        }else{

            return Result.error("500", "token失效,请重新登录");
        }
    }

    /**
     * 检查token 是否正确
     * @param token
     * @return
     */
    @GetMapping("/checkToken")
    public Result<?> checkToken(String token){
        if(JwtUtil.checkToken(token) == true){
            return Result.success("200","token合法",true);
        }
        return Result.error("500", "token失效");
    }


}
